package com.winning.pmph.ctrl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.pmph.entity.ExerciseBook;
import com.winning.pmph.entity.ExerciseEntry;
import com.winning.pmph.entity.ExerciseEntryLib;
import com.winning.pmph.entity.ExerciseGroupLib;
import com.winning.pmph.service.ExerciseBookService;
import com.winning.pmph.service.ExerciseEntryLibService;
import com.winning.pmph.service.ExerciseGroupLibService;
import com.winning.pmph.utils.Const;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Api(value = "知识资源管理/试题管理/题库管理")
@RestController
@RequestMapping("/pmph/action/exerciseLib")
public class ExerciseLibCtrl {
    @Resource
    private ExerciseBookService exerciseBookService;
    @Resource
    private ExerciseEntryLibService exerciseEntryLibService;
    @Resource
    private ExerciseGroupLibService exerciseGroupLibService;

    @ApiOperation(value = "selectExerciseGroupLibPage", notes = "题库管理, 查询试题page接口")
    @GetMapping("selectExerciseGroupLibPage")
    public Page<ExerciseGroupLib> selectExerciseGroupLibPage(ExerciseGroupLib exerciseGroupLib) {
        Page<ExerciseGroupLib> exerciseGroupLibPage = exerciseGroupLibService.selectExerciseGroupLibPage(exerciseGroupLib);
        parseVo(exerciseGroupLibPage);
        return exerciseGroupLibPage;
    }
    @ApiOperation(value = "selectExerciseGroupLibList", notes = "题库管理, 根据实体查询集合")
    @GetMapping("selectExerciseGroupLibList")
    public List<ExerciseGroupLib> selectExerciseGroupLibList(ExerciseGroupLib exerciseGroupLib) {
        QueryWrapper<ExerciseGroupLib> queryWrapper = new QueryWrapper<>(exerciseGroupLib);
        List<ExerciseGroupLib> exerciseGroupLibList = exerciseGroupLibService.getBaseMapper().selectList(queryWrapper);
        for (ExerciseGroupLib groupLib : exerciseGroupLibList) {
            List<ExerciseEntryLib> exerciseEntryLibList = exerciseEntryLibService.lambdaQuery().eq(ExerciseEntryLib::getGroupId, groupLib.getId()).list();
            groupLib.setExerciseEntryList(JSON.parseArray(JSON.toJSONString(exerciseEntryLibList), ExerciseEntry.class));
        }
        return exerciseGroupLibList;
    }

    private void parseVo(Page<ExerciseGroupLib> exerciseGroupLibPage) {
        for (ExerciseGroupLib exerciseGroupLib : exerciseGroupLibPage.getRecords()) {
            if (StringUtils.isNotEmpty(exerciseGroupLib.getExerciseBookId())) {
                ExerciseBook exerciseBook = exerciseBookService.getById(exerciseGroupLib.getExerciseBookId());
                if (Objects.nonNull(exerciseBook)) {
                    exerciseGroupLib.setBookName(exerciseBook.getName());
                }
            }
            else {
                exerciseGroupLib.setBookName(Const.EXERCISE_SOURCE_DIY);
            }
        }
    }

    @ApiOperation(value = "deleteExerciseGroupLib", notes = "删除试题")
    @PostMapping("deleteExerciseGroupLib")
    public void deleteExerciseGroupLib(@RequestBody ExerciseGroupLib exerciseGroupLib) {
        exerciseGroupLibService.deleteExerciseGroupLib(exerciseGroupLib);
    }

    @ApiOperation(value = "selectExerciseEntryLibListByGroupLibId", notes = "试题编辑, 详情, 根据groupId, 查询试题集合")
    @GetMapping("selectExerciseEntryLibListByGroupLibId")
    public List<ExerciseEntryLib> selectExerciseEntryLibListByGroupLibId(String exerciseGroupLibId) {
        return exerciseEntryLibService.lambdaQuery().eq(ExerciseEntryLib::getGroupId, exerciseGroupLibId).list();
    }

    @ApiOperation(value = "saveOrUpdateExerciseGroupLib", notes = "试题库试题编辑, 保存接口")
    @PostMapping("saveOrUpdateExerciseGroupLib")
    public void saveOrUpdateExerciseGroupLib(@RequestBody ExerciseGroupLib exerciseGroupLib) {
        exerciseGroupLibService.saveOrUpdateExerciseGroupLib(exerciseGroupLib);
    }
}